import 'package:bot_toast/bot_toast.dart';
import 'package:easy_video/controller/app_controller.dart';
import 'package:easy_video/internal/api/user_api.dart';
import 'package:easy_video/model/user_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class LoginController extends GetxController {
  final formKey = GlobalKey<FormState>();
  final TextEditingController mobileController = TextEditingController();
  final TextEditingController captchaController = TextEditingController(
    text: "9999",
  );

  void getCaptcha() async {
    BotToast.showText(text: "功能暂时无法使用,请输入9999固定验证码");
    return;
    var mobile = this.mobileController.text;
    if (mobile.isEmpty) {
      BotToast.showText(text: "请输入手机号码");
      return;
    }
  }

  void login() async {
    if (this.formKey.currentState?.validate() ?? false) {
      this.formKey.currentState?.save();
      //请求接口登录
      UserApi()
          .login(this.mobileController.text, this.captchaController.text)
          .then(
        (res) {
          UserModel user = UserModel.fromJson(res.data["data"]);
          String token = res.data["data"]["token"];
          AppController().setUser(user, token).then((_) {
            Get.back();
          });
        },
      );
    }
  }

  @override
  void onClose() {
    super.onClose();
    this.captchaController.dispose();
    this.mobileController.dispose();
  }
}
